<?php namespace Phpcmf\Model\Jubao;

// 模块内容模型类

class Content extends \Phpcmf\Model\Content {


    // 内容发布之前 ===1=10/
    public function _content_post_before($id, $data, $old) {

        // 发布之前
        // 判断是否悬赏
        /*$data[1]['price'] = intval($data[1]['price']);
        if ($data[1]['price']) {
            $max = (int)\Phpcmf\Service::C()->get_cache('module-'.SITE_ID.'-jianyi', 'setting', 'param', 'config', 'max_price');
            if ($data[1]['price'] < 1) {
                \Phpcmf\Service::C()->_json(0, '悬赏金额最低1元');
            } elseif (strpos($data[1]['price'], '.') !== false) {
                \Phpcmf\Service::C()->_json(0, '悬赏金额只能是整数');
            } elseif ($max && $data[1]['price'] > $max) {
                \Phpcmf\Service::C()->_json(0, '悬赏金额不能超过'.$max.'元');
            }
            if (\Phpcmf\Service::C()->member['money'] - $data[1]['price'] < 0) {
                \Phpcmf\Service::C()->_json(0, '余额不足');
            }
        }*/

        return $data;
    }

    // 内容发布之后
    public function _content_post_after($id, $data, $old) {

        // 判断是否悬赏
        /*if ($data[1]['price'] ) {
            // 账户扣除
            $rt = \Phpcmf\Service::M('Pay')->add_money($data[1]['uid'], -$data[1]['price']);
            if (!$rt['code']) {
                \Phpcmf\Service::M()->db->table(SITE_ID.'_'.MOD_DIR)->where('id', $data[1]['id'])->update([
                    'price' => 0,
                ]);
                \Phpcmf\Service::C()->_json(0, $rt['msg']);
            }
            // 增加到交易流水
            \Phpcmf\Service::M('Pay')->add_paylog([
                'uid' => \Phpcmf\Service::C()->member['id'],
                'username' => \Phpcmf\Service::C()->member['username'],
                'touid' => 0,
                'tousername' => '',
                'mid' => 'system',
                'title' => '帖子《'.$data[1]['title'].'》悬赏托管',
                'value' => -$data[1]['price'],
                'type' => 'finecms',
                'status' => 1,
                'result' => '',
                'paytime' => SYS_TIME,
                'inputtime' => SYS_TIME,
            ]);
        }*/
    }

    // 格式化显示内容,用于前端内容详情页面的格式化
    public function _call_show($data) {

        $cache = \Phpcmf\Service::L('cache')->get('app-comment-'.SITE_ID, 'module', MOD_DIR);
        if (!$cache) {
            return $data;
        } elseif (!$cache['use']) {
            return $data;
        }

        if (!isset($cache['order']) || !$cache['order']) {
            $cache['order'] = 'inputtime desc';
        }

        $page = max(1, (int)\Phpcmf\Service::L('input')->get('page'));

        if (IS_API_HTTP) {
            $pagesize = (int)$cache['pagesize_api'];
        } elseif (\Phpcmf\Service::IS_MOBILE()) {
            $pagesize = (int)$cache['pagesize_mobile'];
        } else {
            $pagesize = (int)$cache['pagesize'];
        }
        !$pagesize && $pagesize = 10;

        // 查询数据
        list($list, $total) = $this->get_comment_result($data['id'], $cache['order'], $page, $pagesize, 0, $cache['field']);

        $data['index'] = $data;
        $data['comment_list'] = $list;
        $data['comment_pages'] = $this->_get_pages(\Phpcmf\Service::L('Router')->show_url(\Phpcmf\Service::C()->module, $data, '{page}'), $total, $pagesize);
        $data['comment_cache'] = $cache;

        return $data;
    }

    /**
     * 评论ajax分页 方便二次开发和重写
     */
    protected function _get_pages($url, $total, $pagesize) {

        $config = [];

        $file = 'config/page/'.(\Phpcmf\Service::IS_PC() ? 'pc' : 'mobile').'/ajax.php';
        if (is_file(WEBPATH.$file)) {
            $config = require WEBPATH.$file;
        } elseif (is_file(ROOTPATH.$file)) {
            $config = require ROOTPATH.$file;
        } else {
            $config['next_link'] = '>';
            $config['prev_link'] = '<';
            $config['last_link'] = '>|';
            $config['first_link'] = '|<';
            $config['cur_tag_open'] = '<a class="ds-current">';
            $config['cur_tag_close'] = '</a>';
        }

        $config['base_url'] = $url;
        $config['per_page'] = $pagesize;
        $config['total_rows'] = $total;
        $config['use_page_numbers'] = TRUE;
        $config['query_string_segment'] = 'page';

        return \Phpcmf\Service::L('Page')->initialize($config)->create_links();
    }

    public function author_url($uid) {

        $urlrule = \Phpcmf\Service::C()->get_cache('module-'.SITE_ID.'-jianyi', 'setting', 'param', 'user', 'urlrule');
        if ($urlrule) {
            return dr_url_prefix(str_replace('{id}', $uid, $urlrule), 'jianyi');
        }

        return '/index.php?s=jianyi&c=user&uid='.$uid;
    }

    public function cache($siteid = SITE_ID) {

        $table = $this->prefix.$siteid.'_jianyi_comment';
        if ($this->db->tableExists($table)) {
            if (!$this->db->fieldExists('zuijiadaan', $table)) {
                $this->query_all('ALTER TABLE `{dbprefix}'.SITE_ID.'_jianyi_comment` ADD `zuijiadaan` INT(10) NULL;');
            }
            // 遗漏的字段
            if (!$this->table('field')->where('relatedname', 'comment-module-jianyi')->where('fieldname','zuijiadaan')->counts()) {
                $this->query_sql('INSERT INTO `{dbprefix}field` (`id`, `name`, `fieldname`, `fieldtype`, `relatedid`, `relatedname`, `isedit`, `ismain`, `issystem`, `ismember`, `issearch`, `disabled`, `setting`, `displayorder`) VALUES
    (null, \'最佳答案\', \'zuijiadaan\', \'Text\', 0, \'comment-module-jianyi\', 1, 1, 0, 0, 0, 0, \'{"option":{"fieldtype":"","fieldlength":"","value":"","width":"","css":""},"validate":{"required":"0","pattern":"","errortips":"","check":"","filter":"","formattr":"","tips":""},"is_right":"0"}\', 0);
    ');
            }
        }
    }
}